using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;

public class StringPopupFiled : VisualElement
{
    public new class UxmlFactory : UxmlFactory<StringPopupFiled, VisualElement.UxmlTraits> { }

    private Label mLabel;



    private PopupField<string> mPopupField;

    private List<string> mValueList;
    public List<string> choices

    {
        get { return mValueList; }
        set
        {
            if (mValueList != null && mValueList.Equals(value)) return;

            var strValue = "";
            if (mPopupField != null)
            {
                strValue = mPopupField.value;
                Remove(mPopupField);
            }

            if (value == null || value.Count == 0)
            {
                mPopupField = new PopupField<string>();
            }
            else
            {
                var index = Mathf.Max(0, value.IndexOf(strValue));
                mPopupField = new PopupField<string>(value, index);
            }

            if (mValueChangedCallback != null)
            {
                mPopupField.RegisterValueChangedCallback(mValueChangedCallback);
                if (strValue != mPopupField.value)
                {
                    var evt = ChangeEvent<string>.GetPooled(strValue, mPopupField.value);
                    evt.target = mPopupField;
                    mPopupField.SendEvent(evt);
                }
            }


            mPopupField.style.flexGrow = 1;
            mPopupField.style.flexShrink = 1;
            mPopupField.style.marginLeft = 0;
            mPopupField.style.marginRight = 0;
            mPopupField.style.marginTop = 0;
            mPopupField.style.marginBottom = 0;
            Add(mPopupField);

            mValueList = value;

            if (mValueList == null) mValueList = new List<string>();

        }
    }

    public string popupValue
    {
        get { return mPopupField.value; }
        set
        {
            if (choices.IndexOf(value) >= 0)
            {
                Popup.value = value;
            }
        }
    }
    public int popupIndex
    {
        get { return mPopupField.index; }
        set
        {
            if (value < choices.Count)
            {
                Popup.index = value;
            }
        }
    }


    private EventCallback<ChangeEvent<string>> mValueChangedCallback;

    public bool RegisterValueChangedCallback(EventCallback<ChangeEvent<string>> callback)
    {
        UnregisterValueChangedCallback();
        mValueChangedCallback = callback;
        return mPopupField.RegisterValueChangedCallback(mValueChangedCallback);
    }
    public bool UnregisterValueChangedCallback()
    {
        var ret = false;
        if (mValueChangedCallback != null)
        {
            ret = mPopupField.UnregisterValueChangedCallback(mValueChangedCallback);
            mValueChangedCallback = null;
        }
        return ret;
    }

    public PopupField<string> Popup => mPopupField;

    public StringPopupFiled()
    {
        choices = new List<string>();
    }

    public StringPopupFiled(List<string> choices)
    {
        this.choices = choices;
    }


    public void SyncIndex(ref int index, int defaultIndex = 0)
    {
        if (index < choices.Count)
        {
            Popup.index = index;
        }
        else if(choices.Count > 0)
        {
            Popup.index = 0;
            index = 0;
        }
        else
        {
            index = defaultIndex;
        }
    }
}
